home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Screenblankers / GBlanker / GSource / Blankers / Life / blank.c next >
C/C++ Source or Header  |  1996-09-26  |  9KB  |  366 lines

  1. /*
  2.  *  Copyright (c) 1994 Michael D. Bayne.
  3.  *  All rights reserved.
  4.  *
  5.  *  Please see the documentation accompanying the distribution for distribution
  6.  *  and disclaimer information.
  7.  */
  8.  
  9. #include <exec/memory.h>
  10.  
  11. #include "/includes.h"
  12.  
  13. #ifdef _M68020
  14. #define PREFOBJ LONG
  15. #define UPREFOBJ ULONG
  16. #else
  17. #define PREFOBJ WORD
  18. #define UPREFOBJ UWORD
  19. #endif
  20.  
  21. #define HSIZE ( sizeof( PREFOBJ ) * 8 )
  22. #define HCELLS ( 2 * HSIZE )
  23.  
  24. #define DELAY 0
  25. #define GENS  2
  26. #define DENS  4
  27. #define MODE  6
  28.  
  29. PREFOBJ *Org[2], *Orgs[2], *Lookup;
  30. LONG uw, x1pos, x2pos, ypos, offx, BWid, Hei;
  31. Triplet *ColorTable = 0L;
  32.  
  33. #define RAND( base, offset ) (( ULONG )( RangeRand( base ) + offset ))
  34. #define ORG( v, x, y ) ( Org[v][Lookup[y]+x] )
  35. #define VALUE (( i < HSIZE )? 0L : 1L )
  36.  
  37. UBYTE mask[] = { 96, 240, 240, 96 };
  38. #define RectFill( r, x1, y1, x2, y2 ) BltPattern( r, 0L, x1, y1, x2, y2, 0 )
  39.  
  40. #include "Life_rev.h"
  41. STATIC const UBYTE VersTag[] = VERSTAG;
  42.  
  43. VOID Defaults( PrefObject *Prefs )
  44. {
  45.     Prefs[DELAY].po_Level = 0;
  46.     Prefs[GENS].po_Level = 150;
  47.     Prefs[DENS].po_Level = 16;
  48.     Prefs[MODE].po_ModeID = getTopScreenMode();
  49.     Prefs[MODE].po_Depth = 4;
  50. }
  51.  
  52. VOID SpewRandomShit( PREFOBJ *Org[], PREFOBJ *Orgs[], LONG v )
  53. {
  54.     LONG x = RAND( HCELLS-4, 2 ), y = RAND( Hei-4, 2 ), i, j;
  55.  
  56.     for( j = y; j < y+3; j++ )
  57.         for( i = x; i < x+3; i++ )
  58.             if( RAND( 100, 0 ) > 25 )
  59.             {
  60.                 ORG( v, i, j ) = 1;
  61.                 if( i < HSIZE )
  62.                     Orgs[v][2*j] |= ( 1L << ( (HSIZE-1) - i ));
  63.                 else
  64.                     Orgs[v][2*j+1] |= ( 1L << ( ( HSIZE - 1 ) - i ));
  65.             }
  66. }
  67.  
  68. VOID FillWithRandomShit( struct RastPort *r, PREFOBJ *Org[], PREFOBJ *Orgs[],
  69.                         LONG v, LONG Density )
  70. {
  71.     LONG i, i1, j;
  72.     UPREFOBJ flag;
  73.     
  74.     SetAPen( r, 2L );
  75.  
  76.     for( ypos = 0, j = 0; j < Hei; j++, ypos += uw )
  77.     {
  78.         for( x1pos = offx, x2pos = offx+(uw*HSIZE), i = 0, i1 = HSIZE,
  79.             flag = 1L << (HSIZE-1);    i < HSIZE; i++, i1++, x1pos += uw,
  80.             x2pos += uw, flag = ( 1L << (HSIZE-1) - i ))
  81.         {
  82.             if( RangeRand( 100 ) < Density )
  83.             {
  84.                 ORG( v, i, j ) = 1;
  85.                 Orgs[v][2*j] |= flag;
  86.                 RectFill( r, x1pos, ypos, x1pos + BWid, ypos + BWid );
  87.             }
  88.             if( RangeRand( 100 ) < Density )
  89.             {
  90.                 ORG( v, i1, j ) = 1;
  91.                 Orgs[v][2*j+1] |= flag;
  92.                 RectFill( r, x2pos, ypos, x2pos + BWid, ypos + BWid );
  93.             }
  94.         }
  95.     }
  96. }
  97.  
  98. #define TOP (Hei*2-2)
  99. VOID Grow( PREFOBJ *Orgs, PREFOBJ *OrgsN )
  100. {
  101.     LONG i, j;
  102.     UPREFOBJ mask = 1L << (HSIZE-1), one = 1;
  103.     
  104.     CopyMemQuick( Orgs, OrgsN, 2 * Hei * sizeof( PREFOBJ ));
  105.  
  106.     if( Orgs[0] & one )
  107.         OrgsN[1] |= mask;
  108.     Orgs[0] |= ( Orgs[0] >> 1 );
  109.         if( Orgs[1] & one )
  110.             OrgsN[0] |= mask;
  111.     Orgs[1] |= ( Orgs[1] >> 1 );
  112.     if( Orgs[0] & mask )
  113.         OrgsN[1] |= one;
  114.     Orgs[0] |= ( Orgs[0] << 1 );
  115.     if( Orgs[1] & mask )
  116.         OrgsN[0] |= one;
  117.     OrgsN[1] |= ( Orgs[1] << 1 );
  118.     OrgsN[0] |= Orgs[TOP];
  119.     OrgsN[0] |= Orgs[2];
  120.     OrgsN[1] |= Orgs[TOP+1];
  121.     OrgsN[1] |= Orgs[3];
  122.  
  123.     for( i = 2, j = 3; i < TOP; i += 2, j += 2 )
  124.     {
  125.         OrgsN[i] |= ( Orgs[i] >> 1 );
  126.         OrgsN[j] |= ( Orgs[j] >> 1 );
  127.         OrgsN[i] |= ( Orgs[i] << 1 );
  128.         OrgsN[j] |= ( Orgs[j] << 1 );
  129.         if( Orgs[i] & one )
  130.             OrgsN[j] |= mask;
  131.         if( Orgs[j] & one )
  132.             OrgsN[i] |= mask;
  133.         if( Orgs[i] & mask )
  134.             OrgsN[j] |= one;
  135.         if( Orgs[j] & mask )
  136.             OrgsN[i] |= one;
  137.         OrgsN[i] |= Orgs[i-2];
  138.         OrgsN[i] |= Orgs[i+2];
  139.         OrgsN[j] |= Orgs[j-2];
  140.         OrgsN[j] |= Orgs[j+2];
  141.     }
  142.  
  143.     if( Orgs[TOP] & one )
  144.         OrgsN[TOP+1] |= mask;
  145.     OrgsN[TOP] |= ( Orgs[TOP] >> 1 );
  146.     if( Orgs[TOP+1] & one )
  147.         OrgsN[TOP] |= mask;
  148.     OrgsN[TOP+1] |= ( Orgs[TOP+1] >> 1 );
  149.     if( Orgs[TOP] & mask )
  150.         OrgsN[TOP+1] |= one;
  151.     OrgsN[TOP] |= ( Orgs[TOP] << 1 );
  152.     if( Orgs[TOP+1] & mask )
  153.         OrgsN[TOP] |= one;
  154.     OrgsN[TOP+1] |= ( Orgs[TOP+1] << 1 );
  155.     OrgsN[TOP] |= Orgs[TOP-2];
  156.     OrgsN[TOP] |= Orgs[0];
  157.     OrgsN[TOP+1] |= Orgs[TOP-1];
  158.     OrgsN[TOP+1] |= Orgs[1];
  159. }
  160.  
  161. LONG Blank( PrefObject *Prefs )
  162. {
  163.     struct Screen *LScr;
  164.     struct Window *Wnd;
  165.     struct RastPort *r;
  166.     LONG Gens, RetVal = OK, colrand, maxcol, iter = 1, nbrs, n = 0, m = 1;
  167.     LONG tofront = 0, i, i1, gi, li, j, gj, lj;
  168.     UPREFOBJ flag;
  169.     
  170.     colrand = ( 1L << Prefs[MODE].po_Depth ) - 1;
  171.     if( Prefs[MODE].po_Depth > 2 )
  172.         maxcol = colrand - (1L<<Prefs[MODE].po_Depth)/6;
  173.     else
  174.         maxcol = colrand;
  175.     
  176.     Gens = Prefs[GENS].po_Level;
  177.     
  178.     LScr = OpenScreenTags( 0l, SA_DisplayID, Prefs[MODE].po_ModeID,
  179.                           SA_Depth, Prefs[MODE].po_Depth,
  180.                           SA_Quiet, TRUE, SA_Behind, TRUE,
  181.                           SA_Overscan, OSCAN_TEXT, TAG_DONE );
  182.     if( LScr )
  183.     {
  184.         r = &( LScr->RastPort );
  185.         uw = LScr->Width / HCELLS;
  186.         offx = ( LScr->Width - ( uw * HCELLS ))/ 2;
  187.         Hei = LScr->Height / uw;
  188.         BWid = uw-2;
  189.         
  190.         Org[0] = AllocVec( sizeof( PREFOBJ ) * HCELLS * Hei, MEMF_CLEAR );
  191.         Org[1] = AllocVec( sizeof( PREFOBJ ) * HCELLS * Hei, MEMF_CLEAR );
  192.         Orgs[0] = AllocVec( sizeof( PREFOBJ ) * 2 * Hei, MEMF_CLEAR );
  193.         Orgs[1] = AllocVec( sizeof( PREFOBJ ) * 2 * Hei, MEMF_CLEAR );
  194.         Lookup = AllocVec( sizeof( PREFOBJ ) * Hei, MEMF_CLEAR );
  195.         
  196.         if( Org[0] && Org[1] && Orgs[0] && Orgs[1] && Lookup )
  197.         {
  198.             SetRGB4(&( LScr->ViewPort ), 0, 0, 0, 0 );
  199.             if( Prefs[MODE].po_Depth > 1 )
  200.                 ColorTable = RainbowPalette( LScr, 0L, 1L, 0L );
  201.             else
  202.                 SetRGB4(&( LScr->ViewPort ), 1, 0xF, 0xF, 0xF );
  203.             Wnd = BlankMousePointer( LScr );
  204.             ScreenToFront( LScr );
  205.             
  206.             for( i = 1; i < Hei; i++ )
  207.                 Lookup[i] = Lookup[i-1] + HCELLS;
  208.             
  209.             FillWithRandomShit( r, Org, Orgs, n, Prefs[DENS].po_Level );
  210.             
  211.             while( RetVal == OK )
  212.             {
  213.                 WaitTOF();
  214.  
  215.                 if(!( tofront++ % 60 ))
  216.                     ScreenToFront( LScr );
  217.  
  218.                 if( !Prefs[DELAY].po_Level ||
  219.                    !( tofront % Prefs[DELAY].po_Level ))
  220.                 {
  221.                     if( !iter )
  222.                         SpewRandomShit( Org, Orgs, n );
  223.                     
  224.                     Grow( Orgs[n], Orgs[m] );
  225.  
  226.                     CopyMemQuick( Org[n], Org[m],
  227.                                  HCELLS * Hei * sizeof( PREFOBJ ));
  228.                     
  229.                     for( ypos = 0, j = 0; j < Hei; j++, ypos += uw )
  230.                     {
  231.                         for( x1pos = offx, x2pos = offx+(uw*HSIZE), i = 0,
  232.                             i1 = HSIZE; i < HSIZE;
  233.                             i++, i1++, x1pos += uw, x2pos += uw )
  234.                         {
  235.                             flag = ( UPREFOBJ )( 1L << ( HSIZE - 1 - i ));
  236.                             if( Orgs[m][2*j] & flag )
  237.                             {
  238.                                 gi = i + 1;
  239.                                 li = ( i + HCELLS - 1 )%HCELLS;
  240.                                 gj = ( j + 1 )%Hei;
  241.                                 lj = ( j + Hei - 1 )%Hei;
  242.                                 nbrs = 0;
  243.                                 if( ORG( n, gi, gj )) nbrs++;
  244.                                 if( ORG( n, i, gj )) nbrs++;
  245.                                 if( ORG( n, li, gj )) nbrs++;
  246.                                 if( ORG( n, gi, j )) nbrs++;
  247.                                 if( ORG( n, li, j )) nbrs++;
  248.                                 if( ORG( n, gi, lj )) nbrs++;
  249.                                 if( ORG( n, i, lj )) nbrs++;
  250.                                 if( ORG( n, li, lj )) nbrs++;
  251.                                 switch( nbrs )
  252.                                 {
  253.                                 case 3:
  254.                                     if( ORG( m, i, j ) < maxcol ) {
  255.                                         SetAPen( r, ( LONG )++ORG( m, i, j ));
  256.                                         RectFill( r, x1pos, ypos, x1pos + BWid,
  257.                                                  ypos + BWid );
  258.                                     } break;
  259.                                 case 2:
  260.                                     if( ORG( m, i, j )) {
  261.                                         if( ORG( m, i, j ) < maxcol ) {
  262.                                             SetAPen( r,
  263.                                                     ( LONG )++ORG( m, i, j ));
  264.                                             RectFill( r, x1pos, ypos, x1pos +
  265.                                                      BWid, ypos + BWid );
  266.                                         }
  267.                                     } else
  268.                                         Orgs[m][2*j] &= ~flag;
  269.                                     break;
  270.                                 default:
  271.                                     ORG( m, i, j ) = 0;
  272.                                     Orgs[m][2*j] &= ~flag;
  273.                                     if( ORG( n, i, j ))    {
  274.                                         SetAPen( r, 0L );
  275.                                         RectFill( r, x1pos, ypos, x1pos + BWid,
  276.                                                  ypos + BWid );
  277.                                     }
  278.                                 }
  279.                             }
  280.                             else
  281.                                 ORG( m, i, j ) = 0;
  282.                             if( Orgs[m][2*j+1] & flag )
  283.                             {
  284.                                 gi = ( i1 + 1 )%HCELLS;
  285.                                 li = ( i1 - 1 )%HCELLS;
  286.                                 gj = ( j + 1 )%Hei;
  287.                                 lj = ( j + Hei - 1 )%Hei;
  288.                                 nbrs = 0;
  289.                                 if( ORG( n, gi, gj )) nbrs++;
  290.                                 if( ORG( n, i1, gj )) nbrs++;
  291.                                 if( ORG( n, li, gj )) nbrs++;
  292.                                 if( ORG( n, gi, j )) nbrs++;
  293.                                 if( ORG( n, li, j )) nbrs++;
  294.                                 if( ORG( n, gi, lj )) nbrs++;
  295.                                 if( ORG( n, i1, lj )) nbrs++;
  296.                                 if( ORG( n, li, lj )) nbrs++;
  297.                                 switch( nbrs )
  298.                                 {
  299.                                 case 3:
  300.                                     if( ORG( m, i1, j ) < maxcol )
  301.                                     {
  302.                                         SetAPen( r, ( LONG )++ORG( m, i1, j ));
  303.                                         RectFill( r, x2pos, ypos, x2pos + BWid,
  304.                                                  ypos + BWid );
  305.                                     } break;
  306.                                 case 2:
  307.                                     if( ORG( m, i1, j ))
  308.                                     {
  309.                                         if( ORG( m, i1, j ) < maxcol )
  310.                                         {
  311.                                             SetAPen( r,
  312.                                                     ( LONG )++ORG( m, i1, j ));
  313.                                             RectFill( r, x2pos, ypos, x2pos +
  314.                                                      BWid, ypos + BWid );
  315.                                         }
  316.                                     }
  317.                                     else
  318.                                     Orgs[m][2*j+1] &= ~flag;
  319.                                     break;
  320.                                 default:
  321.                                     ORG( m, i1, j ) = 0;
  322.                                     Orgs[m][2*j+1] &= ~flag;
  323.                                     if( ORG( n, i1, j ))
  324.                                     {
  325.                                         SetAPen( r, 0L );
  326.                                         RectFill( r, x2pos, ypos, x2pos + BWid,
  327.                                                  ypos + BWid );
  328.                                     }
  329.                                 }
  330.                             }
  331.                             else
  332.                                 ORG( m, i1, j ) = 0;
  333.                         }
  334.                     }
  335.                     
  336.                     n = m;
  337.                     m = 1 - m;
  338.                     iter = (iter+1) % Gens;
  339.                 }
  340.                 RetVal = ContinueBlanking();
  341.             }
  342.             if( Prefs[MODE].po_Depth )
  343.                 RainbowPalette( 0L, ColorTable, 1L, 0L );
  344.             UnblankMousePointer( Wnd );
  345.             CloseScreen( LScr );
  346.         }
  347.         else
  348.             RetVal = FAILED;
  349.  
  350.         if( Org[0] )
  351.             FreeVec( Org[0] );
  352.         if( Org[1] )
  353.             FreeVec( Org[1] );
  354.         if( Orgs[0] )
  355.             FreeVec( Orgs[0] );
  356.         if( Orgs[1] )
  357.             FreeVec( Orgs[1] );
  358.         if( Lookup )
  359.             FreeVec( Lookup );
  360.     }
  361.     else
  362.         RetVal = FAILED;
  363.     
  364.     return RetVal;
  365. }
  366.